package dialogs
{
	import Classes.pi_events;
	
	import UIFree.ObjectEvent;
	import UIFree.components.uif_window;
	import UIFree.controls.uif_label;
	import UIFree.controls.uif_list;
	import UIFree.uif_component;
	
	import cardsbody.pi_pngcard;
	
	import editor.pi_cardPreview;
	
	import flash.events.Event;
	import flash.geom.Rectangle;
	
	import templates.pi_effects;

	public class pi_effectDialog extends uif_window {
		private var _list: uif_list;
		
		public function pi_effectDialog(parent:uif_component, ax:int=0, ay:int=0, styleClass:String='', modal:Boolean=false) {
			super(parent, ax, ay, 260, 400, styleClass, modal);
			var inner: Rectangle = this.getInner();
			with (new uif_label(this, inner.x, 0, inner.width, 100, 'description')) {
				text = pi_data.EFFECTSSPEC;
				alignApply(['left', 'top']);
			}

			_list = new uif_list(this, inner.x, inner.y + 110, inner.width, 270, 'list');
			_list.attachList(pi_effects.list, function(area : uif_component, item: Object, index: int): Object {
				var inner: Rectangle = area.getInner();
				var c_item: uif_label = new uif_label(area, inner.x, inner.y + index * 20, inner.width, 20, 'listItem');
				c_item.text = item.name;
				return c_item;
			});
			
			_list.addEvent('SELECT', doSelect);		
			
			addEvent(pi_events.FULLVIEWCHANGE, function(e: Event): void {
				if (pj.isFullView) doClose(null);
			}, app);
			
			addEvent(Event.SELECT, function(e: ObjectEvent): void {
				if (e.object is pi_cardPreview) {
					if (e.object.data.maskClass != pi_pngcard) doClose(null);
					selectEffect(0);
				}
			}, app);
		}
		
		private function doSelect(e: Event): void {
			if (curEffectID != _list.selectItem._data.id)
				app.dispatchEvent(new ObjectEvent(pi_events.CHANGEEFFECT, _list.selectItem._data.id));
		} 
		
		private function selectEffect(effectID: int): void {
			_list.list.forEach(function(element: Object, index:int, arr:Array): void {
				if (element.id == effectID) _list.selectIndex = index;
			});
		}
		
		protected function get pj(): photo_inject {
			return app as photo_inject; 
		}
		
		protected function get curEffectID(): int {
			return (pj.editor.cardBody as pi_pngcard)?(pj.editor.cardBody as pi_pngcard).effectID:0;	
		}
		
		override public function show(onShow: Function=null): void {
			selectEffect(curEffectID);
			super.show(onShow);
		}
		
		override protected function canClose(): int {
			return 1;
		}
		
	}
}